import {
  type MapFeatures,
  registerMapLegend,
  GeojsonLayers,
  addLayer,
  appendMapFeatures
} from '@/map'
import type { AuxGraphicItem } from './base'

export const auxGraphicLayer = new GeojsonLayers('aux-graphic', '辅助图形')
auxGraphicLayer.setSymbolFontScale(0.7)

export function updateAuxGraphicLayer(items: AuxGraphicItem[]) {
  const mf: MapFeatures = {}
  for (const item of items) {
    appendMapFeatures(mf, item.feature)
  }
  auxGraphicLayer.update(mf)
}

export function setupAuxGraphicLayer() {
  addLayer(auxGraphicLayer)
  registerMapLegend({
    name: '辅助图形',
    visible: () => auxGraphicLayer.visible,
    setVisible: (ok) => (auxGraphicLayer.visible = ok)
  })
}
